<template>
    <div class="page-login">
        <div class="login-box">
            <div class="login-title">
                <h3>项目名称</h3>
                <p>登录</p>
            </div>
            <div class="login-form">
                <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="90px">
                    <el-form-item label="用户名" prop="name">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" type='password'></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit('ruleForm')">登陆</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    data() {
      return {
        ruleForm: {
          name: '',
          password:''         
        },
        rules: {
            name: [
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ]
        }
      };      
    },    
    methods: {
        onSubmit(formName) {            
            this.$refs[formName].validate((valid) => {
            if (valid) {               
                console.log(this.ruleForm.name); 
                console.log(this.ruleForm.password);  
                axios.get('http://cmdcmd123.com').then(res =>{
                    this.data = res.data;
                    console.log(this.data['name']);
                    console.log(this.data['boolean']);
                    if(this.data['boolean'])
                    {
                        this.$notify({
                            title: '成功',
                            message: '登陆成功mockjs模拟',
                            type: 'success'
                        }); 
                        // localStorage.setItem('ms_username',this.data['name']);
                        localStorage.setItem('ms_username',this.ruleForm.name);                        
                        this.$router.push('/readme');
                    }
                    else
                    {
                        this.$notify({
                            title: '失败',
                            message: '账号或密码错误mockjs模拟',
                            type: 'error'
                        }); 
                    }
                }  
                );
                
         
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
            console.log('reset');
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang='less' scoped>
html, body, .page-login {
    min-height: 100vh;
    .page-login {
        background-color: #f7f7f7;
        padding-top: 50px;
        .login-box {
            margin: 0 auto;
            max-width: 400px;
            border-radius: 5px;
            background-color: #fff;
        }
        .login-title {
            color: #2a323c;
            text-align: center;
            padding: 20px 0 0;
            h3 {
                margin: 0 0 30px 0;
            }
            p {
                font-weight: bold;
                color: #898989;
                margin: 0;
            }
        }
        .login-form{
            padding: 20px 30px;
            border: 0;           
        }
    }
}
</style>